package util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import org.apache.log4j.Logger;

public class FileUtil {
	private static Logger logger = Logger.getLogger(FileUtil.class);

	public FileUtil() {

	}

	/**
	 * 
	 */
	public static boolean writeFile(String fileContent, String targetFile,
			String encode, boolean append) {
		Writer stdOut = null;
		FileOutputStream fout = null;
		OutputStreamWriter oout = null;
		File file = null;
		try {
			System.out.println("存放文本开始");
			file = new File(targetFile);
			file.getParentFile().mkdirs();
			fout = new FileOutputStream(file, append);
			oout = new OutputStreamWriter(fout, encode);
			stdOut = new BufferedWriter(oout);
			stdOut.write(fileContent);
			System.out.println("存入文本结束");
		} catch (IOException e) {
			logger.error(e);
			return false;
		} finally {
			try {
				if (stdOut != null) {
					stdOut.close();
					stdOut = null;
				}

				if (oout != null) {
					oout.close();
					oout = null;
				}

				if (fout != null) {
					fout.close();
					fout = null;
				}
				if (file != null) {
					file = null;
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		return true;
	}

	/**
	 */
	public static boolean writeFileByLine(String fileContent,
			String targetFile, String encode, boolean append) {
		Writer stdOut = null;
		FileOutputStream fout = null;
		OutputStreamWriter oout = null;
		File file = null;
		try {
			file = new File(targetFile);
			file.getParentFile().mkdirs();
			fout = new FileOutputStream(file, append);
			oout = new OutputStreamWriter(fout, encode);
			stdOut = new BufferedWriter(oout);
			stdOut.write(fileContent + "\r\n");
		} catch (IOException e) {
			logger.error(e);
			return false;
		} finally {
			try {
				if (stdOut != null) {
					stdOut.close();
					stdOut = null;
				}

				if (oout != null) {
					oout.close();
					oout = null;
				}

				if (fout != null) {
					fout.close();
					fout = null;
				}
				if (file != null) {
					file = null;
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * @param bytes
	 * @param targetFilel
	 * @return
	 */
	public static boolean writeFile(byte[] bytes, String targetFile) {
		FileOutputStream fout = null;
		BufferedOutputStream os = null;
		try {
			fout = new FileOutputStream(targetFile);
			os = new BufferedOutputStream(fout);
			os.write(bytes);
		} catch (FileNotFoundException e) {
			logger.error(e);
			return false;
		} catch (IOException e) {
			logger.error(e);
			return false;
		}
		return true;
	}

	/**
	 * @param bytes
	 * @param targetFilel
	 * @return
	 */
	public static boolean writeUTF8File(byte[] bytes, String oldEncode,
			String targetFile) {
		String context;
		boolean isSuccess = false;
		try {
			context = new String(bytes, oldEncode);
			isSuccess = writeFile(context, targetFile, "UTF-8", false);
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
		}
		return isSuccess;
	}

	/**
	 * @param in
	 * @param targetFile
	 */
	public static void writeFile(InputStream in, String targetFile) {
		FileOutputStream fout = null;
		BufferedOutputStream os = null;
		try {
			fout = new FileOutputStream(targetFile);
			os = new BufferedOutputStream(fout);
			int b = 0;
			while ((b = in.read()) != -1) {
				os.write(b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
					os = null;
				}

				if (fout != null) {
					fout.close();
					fout = null;
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param fileName
	 * @return
	 */
	public static byte[] readFile(String fileName) {

		try {
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(fileName));

			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			int ch;
			while ((ch = in.read()) != -1)
				buffer.write(ch);
			return buffer.toByteArray();
		} catch (IOException e) {

			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param romatefilename
	 */
	public String readRomateFile(String romatefilename) {
		URL urlfile;
		BufferedReader in;
		String content = "";
		String inputLine;
		try {
			urlfile = new URL(romatefilename);
			in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
			inputLine = in.readLine();
			while (inputLine != null) {
				content += inputLine;
				inputLine = in.readLine();
			}
			System.out.println(content);
			in.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;

	}

	/**
	 * 
	 * @param fileName
	 * @return
	 */
	public static byte[] readFile(File file) {

		try {
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(file));

			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			int ch;
			while ((ch = in.read()) != -1)
				buffer.write(ch);
			return buffer.toByteArray();
		} catch (IOException e) {

			e.printStackTrace();
			return null;
		}
	}

	public static Vector getFileList(String path) {

		File file = new File(path);
		Vector fileList = new Vector();
		File[] tmp = file.listFiles();
		for (int i = 0; i < tmp.length; i++) {
			if (tmp[i].isFile()) {
				fileList.add(tmp[i]);
			} else if (tmp[i].isDirectory()) {
				fileList.addAll(getFileList(tmp[i].getAbsolutePath()));
			}
		}
		return fileList;
	}

	public static String LoadContentByInputStream(InputStream is) {
		String content = "";
		try {
			String s1 = new String();
			BufferedReader bufferedreader = new BufferedReader(
					new InputStreamReader(is, "utf-8"));
			while ((s1 = bufferedreader.readLine()) != null) {
				content += s1;
			}
			bufferedreader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}

	public static String transformUTF(String str) {

		try {
			if ((str == null) || str.equals("")) {
				return "";
			}
			byte[] temp = str.getBytes("GBK");
			str = new String(temp, "UTF-8");

			return str;
		} catch (Exception e) {
			return "";
		}
	}

	public static String[] getFileNameBySuffix(String dirPath,
			final String suffix) {
		File f = new File(dirPath);
		String[] fileName = f.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.endsWith(suffix)) {
					return true;
				}
				return false;
			}
		});

		return fileName;
	}

	public static void main(String[] args) {
		String path = "F:\\rss\\70";

		Vector list = FileUtil.getFileList(path);
		for (int i = 0; i < list.size(); i++) {
			File file = (File) list.get(i);
			System.out.println(file.getAbsolutePath());
		}
	}
}
